#include "widget.h"
#include "ui_widget.h"


#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>





Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //打印当前路径
    qDebug() << QApplication::applicationDirPath();

    //初始化变量
    m_sqlite = new CSqlite("hello.db","tb_computer");
    //创建数据表格
    QString sql = "CREATE TABLE IF NOT EXISTS tb_computer "
                  "(id int primary key,name text not null,alias text not null,price real,unit text not null);";

    m_sqlite->doquery(sql);


    //初始化控件
    init();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::init()
{

    //创建控件并放入栅格布局中
    QGridLayout *grid = new QGridLayout();
    for(int i=0;i<m_titleList.count();i++) {
        auto label = new QLabel(m_titleList.at(i));
        auto textBox = new QLineEdit();
        m_fieldList.append(textBox);
        grid->addWidget(label,i,0);
        grid->addWidget(textBox,i,1);
    }
    //创建button控件放入水平布局
    QHBoxLayout *hbox = new QHBoxLayout();
    m_btn_clean = new QPushButton("清空");
    m_btn_search = new QPushButton("查询");
    m_btn_update = new QPushButton("修改");
    m_btn_ok = new QPushButton("提交");
    hbox->addWidget(m_btn_clean);
    hbox->addWidget(m_btn_search);
    hbox->addWidget(m_btn_update);
    hbox->addWidget(m_btn_ok);

    //创建整体布局
    QVBoxLayout *vbox = new QVBoxLayout();
    vbox->addLayout(grid);
    vbox->addLayout(hbox);
    this->setLayout(vbox);

    //调整窗体大小
    setMaximumHeight(200);
    setMaximumWidth(350);

    //设置信号和槽
    connect(m_btn_ok,&QPushButton::clicked,this,[this](){

        //获取所有字段数据并打包成QMap
        QMap<QString,QString> res;
        for(int i=0;i<m_titleList.count();i++){
            res[m_titleList.at(i)] = m_fieldList.at(i)->text();
        }
        bool ret = m_sqlite->addRecord(res);
        if (ret){

            QMessageBox::information(this,"信息","插入数据成功")     ;
            //清空所有数据
            m_btn_clean->click();
            //第一个textbox获取焦点
            m_fieldList.at(0)->setFocus();
        }
        else
        {
            QMessageBox::information(this,"信息","失败")     ;
        }

    }


);

    //清空所有textbox
    connect(m_btn_clean,&QPushButton::clicked,this,[=](){

        foreach (auto item, m_fieldList) {
            item->clear();
        }

    });

    //列出数据库所有数据
    connect(m_btn_search,&QPushButton::clicked,this,[=](){

        auto rows = m_sqlite->getRows();
        qDebug() << QJsonDocument(rows).toJson(QJsonDocument::Indented).data();
    });

    //修改记录
    connect(m_btn_update,&QPushButton::clicked,this,[=](){

        auto id = QInputDialog::getInt(this,"输入关键字ID","请输入关键字ID",1);
        auto res = m_sqlite->getRows(tr("id=%1").arg(id));
        if(res.isEmpty())
            return ;

        QJsonObject row = res[0].toObject();
        //将字段填充
        for(int i=0;i<m_titleList.count();i++) {
            //获取字段值
            auto value = row[m_titleList.at(i)].toString();
            //将字段值填入相对应textbox
            m_fieldList.at(i)->setText(value);
        }


    });

}
